Image Recognition Demo
- Demo1: Image recognition
This demo includes the following parts:
1) Background calibration before data collection (the influence of background on image recognition can be excluded);
2) Take photos to collect data, and train data to generate models;
3) Use models to identify images.
from DobotEDU import *
import base64
import cv2
import time
def to_base64(file_name): #Convert to Base64 format
with open(file_name, 'rb') as f:
base64_data = base64.b64encode(f.read())
return base64_data
def get_image(file_name, timeout, port, flip=False):
pic = util.get_image(timeout, port, flip)
cv2.imwrite(file_name, pic)
base64_image = to_base64(file_name).decode("utf-8")
return base64_image
#Image recognition
#Background image
back_ground = get_image("D:/1.png", 5, 0)
#Background calibration
ai.set_background(back_ground)
cv2.waitKey(0)
#Set up data sets
size = 3 #Number of data sets
data_base = {}
for j in range(size):
a = input("Please input tags:") #Name of data sets
print(a)
data_base[j] = a
#0, 1, 2
a_t = input("Please input number of photos of data sets:")
a_i= int(a_t)
for i in range(a_i):
img = get_image("D:/1.png", timeout=5, port=0)
cv2.waitKey(0)
if i == 0 and j == 0:
res = ai.feature_image_classify([img], [j], size, 0)
print(res)
elif i == a_i-1 and j == size - 1:
haha = ai.feature_image_classify([img], [j], size, 2)
else:
ai.feature_image_classify([img], [j], size, 1)
#Take photos to acquire pictures to be recognized
while True:
print("data_base", data_base)
image = get_image("D:/1.png", timeout=5, port=0)
cv2.waitKey(0)
#Call moodels to recognize
res = ai.feature_image_group(image)
result = data_base[res]
print("image recognition result", result)
- Demo2: Save files after recognition
This demo describes how to save and delete a data set. It contains:
1) If the data already exists, you can choose whether to delete and re-collect the data or retain the original data for direct training (no need to re-collect the data);
2) Data saving path (can be customized);
3) Continue to identify and output results after updating or retaining data.
#New function: Save files after recognition
#Image recognition
from DobotEDU import *
import base64
import cv2
import time
import os
def to_base64(file_name): #Convert to Base64 format
with open(file_name, 'rb') as f:
base64_data = base64.b64encode(f.read())
return base64_data
def get_image(file_name, timeout, port, flip=False):
pic = util.get_image(timeout, port, flip)
cv2.imwrite(file_name, pic)
base64_image = to_base64(file_name).decode("utf-8")
return base64_image
#Delete files
def del_file(path):
ls = os.listdir(path)
for i in ls:
c_path = os.path.join(path, i)
if os.path.isdir(c_path):
os.rmdir(c_path)
else:
os.remove(c_path)
#Image recognition
#Judge whether background calibration is needed
while True:
is_cal = input("whether background calibration is needed?(y/n)")
if (is_cal == "y") or (is_cal == "Y"):
# Background image
back_ground = get_image("D:/1.png", 5, 0)
# Background calibration
ai.set_background(back_ground)
cv2.waitKey(0)
break
elif (is_cal == "n") or (is_cal == "N"):
break
else:
print("Please input y or n.")
my_path = "D:\\temp\\userdir" #Modify the path of photo filefolder
if not (os.path.exists(my_path)):
os.mkdir(my_path)
else:
is_tra = input("Catalogue already exists. Do you directly train existing data?(y or n)")
while True:
if (is_tra == "y") or (is_tra == "Y"):
# Direct training
files_dir = os.listdir(my_path)
size = len(files_dir)
data_base = {}
print("To train data in the following filefolders:", files_dir)
for j in range(size):
data_base[j] = files_dir[j]
sub_dir = os.path.join(my_path, files_dir[j])
if os.path.isdir(sub_dir):
sub_files = os.listdir(sub_dir)
a_i = len(sub_files)
for i in range(a_i):
img = to_base64(os.path.join(sub_dir, sub_files[i])).decode("utf-8")
if i == 0 and j == 0:
res = ai.feature_image_classify([img], [j], size, 0)
elif i == a_i - 1 and j == size - 1:
haha = ai.feature_image_classify([img], [j], size, 2)
else:
ai.feature_image_classify([img], [j], size, 1)
break
elif (is_tra == "n") or (is_tra == "N"):
# Train after generating photo data
is_del = input("Delete all photo data under the catologue?(y/n)")
if (is_del == "y") or (is_del == "Y"):
del_file(my_path)
print("Deleted successfully")
size = int(input("Please input number of data sets:"))
data_base = {}
for j in range(size):
a = input("Please input tags:") # Name of data sets
temp_path = my_path + ("\\%s" % a)
if not os.path.exists(temp_path):
os.makedirs(temp_path)
data_base[j] = a
# 0, 1, 2
a_t = input("Please input number of photos of data sets:")
a_i = int(a_t)
for i in range(a_i):
img = get_image(temp_path + "\\%d.png" % i,
timeout=5,
port=0)
cv2.waitKey(0)
if i == 0 and j == 0:
res = ai.feature_image_classify([img], [j], size, 0)
elif i == a_i - 1 and j == size - 1:
haha = ai.feature_image_classify([img], [j], size, 2)
else:
ai.feature_image_classify([img], [j], size, 1)
break
else:
print("Please input y or n.")
#Take photos to aquire photos to be recognized
while True:
print("data_base", data_base)
image = get_image("D:/1.png", timeout=5, port=0)
cv2.waitKey(0)
#Call models to recognize
res = ai.feature_image_group(image)
result = data_base[res]
print("image recognition result:", result)
- Demo3: Color segmentation
This demo can be used in picture segmentation according to colors and identifying objects in different colors.
#Color segmentation
#version: Python3
from DobotEDU import *
import base64
import cv2
import time
def to_base64(file_name): #Convert to Base64 format
with open(file_name, 'rb') as f:
base64_data = base64.b64encode(f.read())
return base64_data
def get_image(file_name, timeout, port, flip=False):
pic = util.get_image(timeout, port, flip)
cv2.imwrite(file_name, pic)
base64_image = to_base64(file_name).decode("utf-8")
return base64_image, pic
#Background image
back_ground = get_image("D:/1.png", 5, 0)
#Background calibration
ai.set_background(back_ground[0])
cv2.waitKey(0)
size = int(input("Please input name of data sets:")) #Number of data sets
data_base = {}
for j in range(size):
# Popup photo segmentation
a = input("Please input name of data sets:")
print(a)
data_base[j] = a
res = get_image("D:/1.png", 5, 0)
resq = ai.color_image_cut(res[0])
print("color_image_cut: ", resq)
#Display segmentation result
if len(resq) == 0:
print("No objects to be cut")
else:
for i in range(len(resq)):
cv2.rectangle(res[1], (resq[i][2], resq[i][3]), (resq[i][4], resq[i][5]),(0,255,0),3)
img = res[1].copy()
dst = cv2.putText(res[1], str(i), (resq[i][0], resq[i][1]), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 4, 4)
cv2.imshow("showImageResult", dst)
cv2.waitKey(0)
#Display cutting picture, input format 0123,12,3
#Training failed. Enter f.
a = input("What photos do you want to add to data set for training:")
a_li = list(a)
#a_li = ['0', '1', '2']
#Convert a to list, format['0', '1', '2']
#Store photo data after cutting
if a[0] == 'f':
print("Exit training")
else:
cut_all = []
if len(a_li) == 0:
print("No photos to be selected")
else:
for k in range(len(a_li)):
d = int(a_li[k]) # select Rectangular Box N
#Access photo data in selected rectangular box
#Different fron shape cutting. The front end displays 2,3,4,5, and data transmitted to the port is 6,7,8,9
cut = img[resq[d][7]:resq[d][9], resq[d][6]:resq[d][8]]
cv2.imwrite('D:/cut.jpg', cut)
#cv2.imshow("cut image",cut)
#cv2.waitKey(0)
base64_data = to_base64('D:/cut.jpg').decode("utf-8")
cut_all.append(base64_data)
if len(cut_all) == 1:
res = ai.color_image_classify([cut_all[0]], [0], size, 2)
print("color_image_classify: ", res)
elif len(cut_all) == 0:
print("No photos to be stored")
else:
for m in range(len(cut_all)):
if m == 0 and j == 0:
a = ai.color_image_classify([cut_all[m]], [j], size, 0)
print(999, a)
elif m == len(cut_all)-1 and j == size-1:
ai.color_image_classify([cut_all[m]], [j], size, 2)
else:
ai.color_image_classify([cut_all[m]], [j], size, 1)
#Take photos to aquire photos to be recognized
print("Training ends. Please start cutting recognition")
img_end = get_image('D:/2.png', timeout=5, port=0)
#Segmentation
resq = ai.color_image_cut(img_end[0])
print(resq)
#Display segmentation result
if len(resq) == 0:
print("No photos to be recognized")
else:
for i in range(len(resq)):
cv2.rectangle(img_end[1], (resq[i][2], resq[i][3]), (resq[i][4], resq[i][5]),(0,255,0),3)
cut = img_end[1][resq[i][3]:resq[i][5], resq[i][2]:resq[i][4]]
cv2.imwrite('D:/cut.jpg', cut)
base64_data = to_base64('D:/cut.jpg').decode("utf-8")
#img = img_end[1].copy()
res = ai.color_image_group(base64_data)
print("color_image_group:", res)
dst = cv2.putText(img_end[1], data_base[int(res)], (resq[i][0], resq[i][1]), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 4, 4)
cv2.imshow("Display result", dst)
cv2.waitKey(0)
#cv2.imshow("showImageResult", dst)
#cv2.waitKey(0)
- Demo4: Shape segmentation
This demo can be used in picture segmentation according to shapes and identifying objects in different shapes.
#version: Python3
from DobotEDU import *
import base64
import cv2
import time
def to_base64(file_name): #Convert to Base64 format
with open(file_name, 'rb') as f:
base64_data = base64.b64encode(f.read())
return base64_data
def get_image(file_name, timeout, port, flip=False): #Take photos to aquire base64 format and np array format of the image
pic = util.get_image(timeout, port, flip)
cv2.imwrite(file_name, pic)
base64_image = to_base64(file_name).decode("utf-8")
return base64_image, pic
#Take photos
back_ground = get_image("D:/1.png", 5, 1)
#Background calibration
ai.set_background(back_ground[0])
cv2.waitKey(0)
data_base = {}
size = int(input("Please input the number of data sets")) #Number of data sets
for j in range(size):
# Popup photo segmentation
a = input("Please input name of data set:")
print(a)
data_base[j] = a
res = get_image("D:/1.png", 5, 1)
resq = ai.image_cut(res[0])
print(resq)
#Display segmentation result
if len(resq) == 0:
print("No photos to be cut")
else:
for i in range(len(resq)):
cv2.rectangle(res[1], (resq[i][2], resq[i][3]), (resq[i][4], resq[i][5]),(0,255,0),3)
img = res[1].copy()
dst = cv2.putText(res[1], str(i), (resq[i][0], resq[i][1]), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 4, 4)
cv2.imshow("showImageResult", dst)
cv2.waitKey(0)
#Display cutting picture, input format 0123
a = input("What photos do you want to add to data set for training:")
a_li = list(a)
#a_li = ['0', '1', '2']
#Convert a to list, format['0', '1', '2']
#Store cutting data
cut_all = []
if len(a_li) == 0:
print("No photos to be selected")
else:
for k in range(len(a_li)):
d = int(a_li[k]) # select Rectangular Box N
# Access photo data in selected rectangular box
cut = img[resq[d][3]:resq[d][5], resq[d][2]:resq[d][4]]
cv2.imwrite('cut.jpg', cut)
base64_data = to_base64('cut.jpg').decode("utf-8")
cut_all.append(base64_data)
if len(cut_all) == 0:
print("No photos to be stored")
else:
for m in range(len(cut_all)):
if m == 0 and j == 0:
ai.feature_image_classify([cut_all[m]], [j], size, 0)
elif m == len(cut_all)-1 and j == size-1:
ai.feature_image_classify([cut_all[m]], [j], size, 2)
else:
ai.feature_image_classify([cut_all[m]], [j], size, 1)
#Take photos to aquire photos to be recognized
print("Training ends. Please start cutting recognition")
img_end = get_image('D:/2.png', timeout=5, port=1)
#Segmentation
resq = ai.image_cut(img_end[0])
print(resq)
#Display segmentation result
if len(resq) == 0:
print("No objects to be recognized")
else:
for i in range(len(resq)):
cv2.rectangle(img_end[1], (resq[i][2], resq[i][3]), (resq[i][4], resq[i][5]),(0,255,0),3)
cut = img_end[1][resq[i][3]:resq[i][5], resq[i][2]:resq[i][4]]
cv2.imwrite('D:/cut.jpg', cut)
base64_data = to_base64('D:/cut.jpg').decode("utf-8")
res = ai.feature_image_group(base64_data)
dst = cv2.putText(img_end[1], data_base[int(res)], (resq[i][0], resq[i][1]), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 4, 4)
cv2.imshow("display result", dst)
cv2.waitKey(0)
#cv2.imshow("showImageResult", dst)
#cv2.waitKey(0)